<?php


namespace app\admin\model;


use think\Model;

class PayUpgrade extends Model
{
	protected $createTime="upgrade_time";
	protected $updateTime="upgrade_oktime";
	public function getUpgradeStateAttr($v){
		$txt = '';
		//0未支付 1支付成功 2支付失败 3处理中 4已退款 5免费升级 6关系升级
		switch ($v){
			case "0":
				$txt = '未支付';
				break;
			case "1":
				$txt = '支付成功';
				break;
			case "2":
				$txt = '支付失败';
				break;
			case "3":
				$txt = '处理中';
				break;
			case "4":
				$txt = '已退款';
				break;
			case "5":
				$txt = '免费升级';
				break;
			case "6":
				$txt = '关系升级';
				break;
		}
		return $txt;
	}

	//统计总成交额
	public function getTotalMoney($calcProfit=false){
		$totalMoney = 0;//总金额
		$cbMoney=0;//成本
		$fMoney=0;//分润总金额
		$profitMoney=0;//利润

		$where=[];
		$query_date = input('query_date', '', 'trim,htmlspecialchars');
		if (!empty($query_date)) {
			$query_date = explode(" - ", $query_date);
			$query_date[0] = strtotime($query_date[0]);
			$query_date[1] = strtotime($query_date[1] . "+1day");
			$where[] = ['upgrade_time', 'between', $query_date];
		}
		$totalMoney = $this->where($where)->whereIn("upgrade_state","1,5,6")->fetchSql(0)->sum("upgrade_money");


		//halt($totalMoney);
		if($calcProfit === true){
			$cbMoney = round($totalMoney*0.006,2);
			$fMoney = model("UserBonuslog")->getUpgradeTotalMoney();
			$profitMoney=$totalMoney-$cbMoney-$fMoney;
		}

		$res = [];
		$res["totalMoney"] = $totalMoney;
		$res["cbMoney"] = $cbMoney;
		$res["fMoney"] = $fMoney;
		$res["profitMoney"] = $profitMoney;
		return $res;
	}
}